import Vue from 'vue';
import Vuex from 'vuex';
import { getVal} from '@/views/basket-vuex/storage.js';


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: getVal(),
    showMenu: false,
    showItem: false,
    activeIndex: -1
  },
  mutations: {
    setList(state, arr) {
      state.list = arr;
    },
    setShowMenu(state, val) {
      state.showMenu = val;
    },
    setShowItem(state, val) {
      state.showItem = val;
    },
    setIndex(state, i) {
      state.activeIndex = i;
    },
    //
    addMenu(state, name) {
      state.list.push({
        name,
        items: []
      })
    },
    // 删除菜
    delMenu(state, index) {
      state.list.splice(index, 1);
    },

    // 添加配料
    addItem(state, item) {
      state.list[state.activeIndex].items.push({
        ...item,
        done: false
      })
    },

    toggleItem(state, obj) {
      const j = state.list[obj.i].items.findIndex(item => item.name === obj.name);
      state.list[obj.i].items[j].done = !state.list[obj.i].items[j].done;

      // console.log(1111111, i, name)
      // const j = state.list[i].items.findIndex(item => item.name === name);
      // state.list[i].items[j].done = !state.list[i].items[j].done;
    }

  },
  getters: {
    menuName: state => (state.list[state.activeIndex] || {}).name
  },
  actions: {
  },
  modules: {
  }
})
